<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Qyoto dialogs</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Qt, dialogs, Qyoto, C#, learn Qyoto">
<meta name="description" content="This chapter of the Qyoto tutorial is 
dedicated to dialogs.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Qyoto dialogs</h1>

<p>
In this part of the Qyoto C# programming tutorial, we will work with dialogs. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Dialog windows or dialogs are an indispensable part of most modern GUI applications. 
A dialog is defined as a conversation between two or more persons. In a computer 
application a dialog is a window which is used to "talk" to the application. 
A dialog is used to input data, modify data, change the application settings etc. 
Dialogs are important means of communication between a user and a computer program. 
</p>


<h2>Message boxes</h2>

<p>
Message boxes are convenient dialogs that provide messages to the user of the 
application. The message consists of text and image data. 
</p>

<pre class="code">
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * This program shows
 * QMessageBox dialogs
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QWidget 
{
    public QyotoApp() 
    {
        WindowTitle = "Message boxes";

        InitUI();

        Resize(220, 90);
        Move(300, 300);
        Show();
    }

    void InitUI() 
    {        
        QGridLayout grid = new QGridLayout(this);
        grid.Spacing = 2;

        QPushButton error = new QPushButton("Error", this);
        QPushButton warni = new QPushButton("Warning", this);
        QPushButton quest = new QPushButton("Question", this);
        QPushButton infor = new QPushButton("Information", this);
        QPushButton about = new QPushButton("About", this);

        grid.AddWidget(error, 0, 0);
        grid.AddWidget(warni, 0, 1);
        grid.AddWidget(quest, 1, 0);
        grid.AddWidget(infor, 1, 1);
        grid.AddWidget(about, 2, 0);

        error.Clicked += ShowDialog;
        warni.Clicked += ShowDialog;
        quest.Clicked += ShowDialog;
        infor.Clicked += ShowDialog;
        about.Clicked += ShowDialog;                
    }

    [Q_SLOT]
    void ShowDialog() 
    {
        QPushButton btn = (QPushButton) Sender();

        if ("Error".Equals(btn.Text)) 
        {
            QMessageBox.Critical(this, "Error", "Error loading file!");
        } else if ("Warning".Equals(btn.Text)) 
        {
            QMessageBox.Warning(this, "Warning", "Operation not permitted!");
        } else if ("Question".Equals(btn.Text)) 
        {
            QMessageBox.Question(this, "Question", "Are you sure to quit?");
        } else if ("Information".Equals(btn.Text)) 
        {
            QMessageBox.Information(this, "Information", "Download completed.");
        } else if ("About".Equals(btn.Text)) 
        {
            QMessageBox.About(this, "About", "ZetCode Qyoto C# tutorial.");
        } 
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();
        return QApplication.Exec();
    }
}
</pre>

<p>
We use the <code>GridLayout</code> manager to set up a grid of five buttons. 
Each of the buttons shows a different message box. 
</p>

<pre class="explanation">
QPushButton button = (QPushButton) Sender();
</pre>

<p>
Here we determine, which button called the <code>ShowDialog()</code> method. 
</p>

<pre class="explanation">
if ("Error".Equals(btn.Text)) 
{
    QMessageBox.Critical(this, "Error", "Error loading file!");
} 
</pre>

<p>
In case we pressed the error button, we show the error dialog. We use static 
methods of the <code>QMessageBox</code> class to show the message boxes.  
</p>


<br>
<img src="/img/gui/sharpqyoto/infodlg.png" alt="Information message dialog">&nbsp;&nbsp;
<img src="/img/gui/sharpqyoto/warningdlg.png" alt="Warning message dialog">&nbsp;&nbsp;
<img src="/img/gui/sharpqyoto/questiondlg.png" alt="Question message dialog">&nbsp;&nbsp;
<img src="/img/gui/sharpqyoto/errordlg.png" alt="Error message dialog">&nbsp;&nbsp;
<img src="/img/gui/sharpqyoto/aboutdlg.png" alt="About message dialog">
<br>


<h2>QInputDialog</h2>

<p>
The <code>QInputDialog</code> class provides a simple convenience dialog to get 
a single value from the user. The input value can be a string, a number or an 
from a list. A label must be set to tell the user what they should enter.
</p>

<pre class="code">
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * This program shows an input
 * dialog.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QWidget 
{
    QLineEdit edit;
    
    public QyotoApp() 
    {
        WindowTitle = "QInputDialog";

        InitUI();

        Resize(300, 150);
        Move(300, 300);
        Show();
    }

    void InitUI() 
    {        
        QPushButton show = new QPushButton("Dialog", this);
        show.Clicked += ShowDialog;

        show.FocusPolicy = Qt.FocusPolicy.NoFocus;
        show.Move(20, 20);

        edit = new QLineEdit(this);
        edit.Move(130, 22);
    }
    
    [Q_SLOT]
    void ShowDialog() 
    {        
        String text = QInputDialog.GetText(
                this, "Input Dialog", "Enter your name");

        if (text!=null &amp;&amp; text.Trim() != String.Empty) 
        {
            edit.Text = text;
        }
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();
        return QApplication.Exec();
    }
}
</pre>

<p>
In the code example, we have a button and a line edit. The button shows an input 
dialog. We get some text and the text is shown in the line edit widget. 
</p>

<pre class="explanation">
String text = QInputDialog.GetText(
        this, "Input Dialog", "Enter your name");
</pre>

<p>
The <code>GetText()</code> static method creates the input dialog.
The text from the dialog is stored in the text variable. 
</p>

<pre class="explanation">
if (text!=null &amp;&amp; text.Trim() != String.Empty) 
{
    edit.Text = text;
}
</pre>

<p>
Before we update the line edit, we ensure, that the text variable is
not null and that it is not empty and does not consists only from spaces. 
</p>


<img src="/img/gui/sharpqyoto/inputdlg.png" alt="Input dialog">
<div class="figure">Figure: Input dialog</div>


<h2>QColorDialog</h2>

<p>
The <code>QColorDialog</code>  class provides a dialog widget for specifying 
colors. The color dialog's function is to allow users to choose colors.
</p>

<pre class="code">
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, we use the
 * QColorDialog to change the colour
 * of a label text.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QWidget 
{
    QLabel label;
    
    public QyotoApp() : base() 
    {
        WindowTitle = "QColorDialog";

        InitUI();

        Resize(250, 200);
        Move(300, 300);
        Show();
    }

    void InitUI() 
    {
        label = new QLabel("ZetCode Qyoto C# tutorial", this);

        QVBoxLayout vbox = new QVBoxLayout(this);
        label.Alignment = AlignmentFlag.AlignCenter;
        vbox.AddWidget(label);
    }    

    protected override void OnMousePressEvent(QMouseEvent arg1) 
    {
        QColor col = QColorDialog.GetColor();

        if (!col.IsValid()) return;

        String style = String.Format("QWidget {{ color: {0} }}", col.Name());
        label.StyleSheet = style;
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();
        return QApplication.Exec();
    }
}
</pre>

<p>
We show a some text in the center of the window. By clicking on the area of the
window, we show a colour dialog. We change the text foreground color to the 
selected colour from the dialog. 
</p>

<pre class="explanation">
protected override void OnMousePressEvent(QMouseEvent arg1) 
{
    ...
}
</pre>

<p>
In order to receive mouse press events for our window, we must override the 
<code>MousePressEvent()</code> method.
</p>

<pre class="explanation">
QColor col = QColorDialog.GetColor();
</pre>

<p>
The <code>QColorDialog</code> is being created. The selected colour is stored in 
the <code>col</code> variable.
</p>

<pre class="explanation">
if (!color.IsValid()) return;
</pre>

<p>
We do nothing, when the cancel button was pressed.
</p>

<pre class="explanation">
String style = String.Format("QWidget {{ color: {0} }}", color.Name());
label.SetStyleSheet(style);
</pre>

<p>
Here we update the foreground color of the label's text.
</p>


<img src="/img/gui/sharpqyoto/colordlg.png" alt="QColorDialog">
<div class="figure">Figure: QColorDialog</div>


<h2>QFontDialog</h2>

<p>
The <code>QFontDialog</code> class provides a dialog widget for selecting a 
font.
</p>

<pre class="code">
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, we use the
 * QFontDialog to change the font
 * of a label text.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QWidget 
{    
    QLabel label;
    
    public QyotoApp() : base() 
    {
        WindowTitle = "QFontDialog";

        InitUI();

        Resize(250, 200);
        Move(300, 300);
        Show();
    }

    void InitUI() 
    {
        label = new QLabel("ZetCode Qyoto C# tutorial", this);

        QVBoxLayout vbox = new QVBoxLayout(this);
        label.Alignment = AlignmentFlag.AlignCenter;
        vbox.AddWidget(label);
    }
    
    protected override void OnMousePressEvent(QMouseEvent arg1) 
    {
        bool ok = true;
        QFont font = QFontDialog.GetFont(ref ok);
        if (!ok) return;
        label.Font = font;
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();
        return QApplication.Exec();
    }
}
</pre>

<p>
This example is similar to the previous one. This time, we change
the font of the text. 
</p>

<pre class="explanation">
QFont font = QFontDialog.GetFont(ref ok);
</pre>

<p>
The <code>QFontDialog</code> is being created. The boolean ok variable is set, 
when we press the OK button of the dialog. 
</p>


<pre class="explanation">
if (!ok) return;
</pre>

<p>
We do nothing, if we didn't press the OK button.
</p>

<pre class="explanation">
label.Font = font;
</pre>

<p>
The <code>font</code> field stores the selected font. We update the label's font
to the newly selected font. 
</p>

<img src="/img/gui/sharpqyoto/fontdlg.png" alt="QFontDialog">
<div class="figure">Figure: QFontDialog</div>


<p>
In this part of the Qyoto C# tutorial, we worked with dialog windows. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<br>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified November 1, 2012  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

